<template>
  <el-card class="single">
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="题目" prop="title">
        <el-input v-model="form.title"></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="form.row" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <el-checkbox v-model="form.must" style="margin-left: 20px"
          >必填</el-checkbox
        >
      </el-form-item>
      <!-- 这里开始 -->
      <el-form-item label="分值">
        <el-input-number
          v-model="form.score"
          :min="1"
          :max="100"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="答案">
        <el-input
          v-model="form.correct_answer"
          placeholder="请输入题目正确答案"
        ></el-input>
        <el-checkbox v-model="hasAnswer" style="margin-left: 20px"
          >设为正确答案</el-checkbox
        >
      </el-form-item>
      <!-- 这里结束 -->
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">确定</el-button>
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
  export default {
    props: {
      closePanel: Function,
      questionnaire_id: Number,
      getQuestionnaire: Function,
      addQuestion: Function,
    },
    data() {
      return {
        form: {
          title: "",
          must: true,
          remark: "",
          questionnaire_id: null,
          question_id: null,
          username: localStorage.getItem("username"),
          type: "fill",
          row: "单行文本题",
          correct_answer: null,
          score: 5,
        },
        rules: {
          title: [{ required: true, message: "请输入题目名称", trigger: "blur" }],
        },
        options: [
          {
            value: 1,
            label: "单行文本题",
          },
          {
            value: 2,
            label: "多行文本题",
          },
        ],
        timer: null,
        hasAnswer: true,
      };
    },
    created() {
      this.form.questionnaire_id = this.questionnaire_id;
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //   执行提交
            this.form.row = this.form.row === "单行文本题" ? "1" : "2";
            if((!this.hasAnswer) || this.form.correct_answer == "") {
              this.form.correct_answer = null
            }
            // axios.post("questions/", this.form).then((response) => {
            //   console.log(response.status);
            //   console.log(response.data);
            // });
            // 这里进行新建问题后的提交
            // 调用父组件的方法
            // 给后端传完整的问卷
            this.form["id"] = null;
            this.addQuestion(this.form);

            clearTimeout(this.timer);
            this.timer = setTimeout(() => {
              this.getQuestionnaire();
              this.closePanel();
            }, 200);
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      cancel() {
        this.closePanel();
      },
    },
  };
</script>

<style scoped>
.el-input {
  width: 50%;
}

.el-form-item {
  display: flex;
  justify-content: center;
}
</style>